#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int a;
    cin >> a;
    string str1;
    string str2;
    cin >> str1 >> str2;
    string strs;
    int j = -1;
    int u = -1;
    int ret = a;
    for (int i = 0; i < a; i++)
    {
        cin >> strs;
        if (str1 == strs)
        {
            if (u != -1)
            {
                ret = min(ret, i - u);
            }
            j = i;
        }
        if (str2 == strs)
        {
            if (j != -1)
            {
                ret = min(ret, i - j);
            }
            u = i;
        }
    }
    if (ret != a)
        cout << ret;
    else
        cout << -1;
    return 0;
}